import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { WisdomGroupMoneyStrategyV2 } from "./WisdomMoneyStrategyV2";
import { NormalStepMoneyStrategy } from "./NormalStepMoneyStrategy";
import { BossRabbitMoneyStrategy, BossRabbitToStepMoneyStrategy, BossThreeRabiatMoneyStrategy } from "./BossRabitMoneyStrategy";
import { WisdomMoneyStrategyV29Group4 } from "./WisdomMoneyStrategyV29Group4";
import { StepMultipleMoneyStrategy } from "./StepMultipleMoneyStrategy";
import { BossRabitRandomStepStrategy } from "./BossRabitRandomStepStrategy";


export class WisdomMoneyStrategyV29Group4V6 extends WisdomMoneyStrategyV29Group4 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV2Debug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 10000;
    protected _subPlanSum: number = 0;
    public get subPlanSum() {
        return this._subPlanSum;
    }
    constructor(name?: string) {
        super(name);
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sum = 0;


        this.subPlan.forEach(pl => {
            ;
            let [rv, rm] = pl.showMoney();
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += (pl.recmInfo.recmMoney * pl.multiple);

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += (pl.recmInfo.recmMoney * pl.multiple);
            }
        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }
    resetRecm() {
        this.subPlan.forEach(item => item.resetRecm());
    }
    resetStrategy(): void {
        this.subPlan.splice(0, this.subPlan.length);
        let tz1 = new ExcutePlan();
        tz1.multiple = 0;
        tz1.planName = "兔正1";
        tz1.recmStrategy = [new FixedStrategy({ reverse: false })];
        tz1.moneyStrategy = [new BossThreeRabiatMoneyStrategy("兔正1", {
            baseMoney: 10,
            winStep: 1,
            failStep: -3,
            currentMoney: 10,
            prevMoney: 0,
            prevPrevMoney: 0,
            maxMoney: 9999999999
        })];
        tz1.toResult(new RoundInfo());
        this.subPlan.push(tz1);

        let tf1 = new ExcutePlan();
        tf1.planName = "兔反1";
        tf1.multiple = 0;
        tf1.recmStrategy = [new FixedStrategy({ reverse: true })];
        tf1.moneyStrategy = [new BossThreeRabiatMoneyStrategy("兔反1", {
            baseMoney: 10,
            winStep: 1,
            failStep: -3,
            currentMoney: 10,
            prevMoney: 0,
            prevPrevMoney: 0,
            maxMoney: 9999999999
        })];
        tf1.toResult(new RoundInfo());
        this.subPlan.push(tf1);


        let tz3 = new ExcutePlan();
        tz3.planName = "兔正3";
        tz3.multiple = 1;
        tz3.recmStrategy = [new FixedStrategy({ reverse: false })];
        tz3.moneyStrategy = [new BossRabitRandomStepStrategy("兔正3", {
            baseMoney: 10,
            prevMoney: 10,
            winStep:-2,
            failStep:1
        })];
        tz3.toResult(new RoundInfo());
        this.subPlan.push(tz3);


        let tf3 = new ExcutePlan();
        tf3.planName = "兔反3";
        tf3.multiple = 1;
        tf3.recmStrategy = [new FixedStrategy({ reverse: true })];
        tf3.moneyStrategy = [new BossRabitRandomStepStrategy("兔反3", {
            baseMoney: 10,
            prevMoney: 10,
            winStep:-2,
            failStep:1
        })];
        tf3.toResult(new RoundInfo());
        this.subPlan.push(tf3);

    }
    setSubResult(pl: ExcutePlan, roundInfo: RoundInfo) {
        pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);

        let m = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
        this._subPlanSum += m;

        let userRecmV = pl.toResult(roundInfo);
        let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
        pl.userRecmInfo.recmV = userRecmV;
        return newRoundInfo;
    }
    toSubMoney(pl: ExcutePlan, roundInfo: RoundInfo) {
        if (pl.running) {
            let [rv, rm] = pl.toMoney(roundInfo, pl.recmInfo, pl.userRecmInfo, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;
            if (rv != EnumBjlResult.U) {
                //pl.recmInfo.
                pl.recmInfo.betCount++;
            }
        }
        else {
            pl.recmInfo.recmV = EnumBjlResult.U;
            pl.recmInfo.recmMoney = 0;
        }

    }

    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let zm = 0;
        let xm = 0;
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        let sumWin = 0;

        let reset = false;
        this._subPlanSum = sumWin;

        let tz1 = this.subPlan[0];
        let tf1 = this.subPlan[1];

        let tz3 = this.subPlan[2];
        let tf3 = this.subPlan[3];


        let tz1MoneyStrategy = tz1.moneyStrategy[0] as BossRabbitMoneyStrategy;
        let tf1MoneyStrategy = tf1.moneyStrategy[0] as BossRabbitMoneyStrategy;
        let tz3MoneyStrategy = tz3.moneyStrategy[0] as BossRabbitToStepMoneyStrategy;
        let tf3MoneyStrategy = tf3.moneyStrategy[0] as BossRabbitToStepMoneyStrategy;



        let tf1RoundInfo = this.setSubResult(tf1, roundInfo);
        this.toSubMoney(tf1, tf1RoundInfo);
        let tz1RoundInfo = this.setSubResult(tz1, roundInfo);
        this.toSubMoney(tz1, tz1RoundInfo);

        let tz3RoundInfo = this.setSubResult(tz3, roundInfo);
        this.toSubMoney(tz3, tz3RoundInfo);

        let tf3RoundInfo = this.setSubResult(tf3, roundInfo);
        this.toSubMoney(tf3, tf3RoundInfo);
       


        this.subPlan.forEach(pl => {
            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += (pl.recmInfo.recmMoney * pl.multiple);

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += (pl.recmInfo.recmMoney * pl.multiple);
            }
        })

        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }


        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV29Group4V6;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            this._subPlanSum = obj._subPlanSum;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}